package com.ultrapower.request;

import java.util.List;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Scan;

import com.sematext.hbase.wd.RowKeyDistributorByHashPrefix;
import com.ultrapower.bean.ApiMetaDataBean;
import com.ultrapower.mapper.ApiMetaDataMapper;
import com.ultrapower.util.HBaseTables;
import com.ultrapower.util.rowkey.RangeOneByteSimpleHash;

/**
 * 
 * @author wangy
 *
 */
public class ApiMetaDataTable extends BaseRequest {
	
	RowKeyDistributorByHashPrefix rowKeyDistributorByHashPrefix = new RowKeyDistributorByHashPrefix(
			new RangeOneByteSimpleHash(0, 32, 8));
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		BaseRequest apiMetaDataTable = new ApiMetaDataTable();
		apiMetaDataTable.execute();
	}

	@Override
	void execute() {
		TableName apiMetaDataTableName = this.getTableName(HBaseTables.API_METADATA_STR);

		this.getHbaseTemplate().get(apiMetaDataTableName, createGet(), (r) -> {
			ApiMetaDataMapper map = new ApiMetaDataMapper(rowKeyDistributorByHashPrefix);
			try {
				List<ApiMetaDataBean> apiMate = map.mapRow(r, 0);
				System.out.println(apiMate);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return null;
		});
	}

	protected Get createGet() {
		int apiId = -130;
		//timestamp=1532501068704L;
		ApiMetaDataBean apiMetaDataBo = new ApiMetaDataBean(agentId, this.startTime, apiId);
		byte[] sqlId = rowKeyDistributorByHashPrefix.getDistributedKey(apiMetaDataBo.toRowKey());
		Get get = new Get(sqlId);
		get.addFamily(HBaseTables.API_METADATA_CF_API);
		return get;
	}

	@Override
	protected Scan createScan() { return null;}

}
